OB.Wms_ActionButton = OB.Wms_ActionButton || {};

var QualityPutdata = {};
var QualityResultId = "";
var QualityView = null;
var QualityWindow = null;
var contreList={};

//质量数据信息录入
OB.Wms_ActionButton.QualityFileResultButton = function(params, view) {
    var grid = view.activeView.viewGrid;
    var data = grid.getSelectedRecord();

    var id = data.id;
    var qtyTest = data.qty;

    var callback = function(rpcResponse, data, rpcRequest) {
        if (data.result) {
            QualityView = view;
            createDeliveryFileNoteWindow(data.data, qtyTest);
            QualityView.refresh();
        }
    };
    OB.RemoteCallManager.call("com.mes.handler.QualityFileResultHandler",{
        id: id
    },{},callback);

}

function createDeliveryFileNoteWindow(data, qtyTest){
    var listGrid = isc.ListGrid.create({
        ID: "createDeliveryNoteForm",
        canEdit: true,
        editByCell: true,
        editEvent: "click",
        width: 800,
        height: 400,
        fields: [
            {width: '25%', title: '检验项目ID', name: 'XMID', canEdit: false, hide: true},
            {width: '1%', title: '仓库ID', name: 'CKID',canEdit: false},
            {width: '1%', title: '库位ID', name: 'KWID',canEdit: false},
            {width: '1%', title: '采购单号', name: 'CGCODE',canEdit: false},
            {width: '1%', title: '采购入库单ID', name: 'CGID',canEdit: false},
            {width: '1%', title: '单位ID', name: 'DWID',canEdit: false},
            {width: '1%', title: '颜色ID', name: 'YSID',canEdit: false},
            {width: '1%', title: '尺码ID', name: 'CMID',canEdit: false},
            {width: '1%', title: '检验detailID', name: 'DETAILID',canEdit: false},
            {width: '1%', title: '供应商ID', name: 'GYSID',canEdit: false},
            {width: '1%', title: '物料ID', name: 'WLID',canEdit: false},
            {width: '45%', title: '物料', name: 'WLNAME',canEdit: false},
            {width: '45%', title: '检验项目', name: 'XMNAME',canEdit: false},
            {width: '20%', title: '到货数量', name: 'QTY_TEST', type: 'number', canEdit: false},
            {width: '20%', title: '不合格数量', name: 'QTY_ERROR',type: 'number'},
            {width: '25%', title: '检验数量', name: 'QTY',type: 'number'},
            {width: '25%', title: '检验结果', name: 'RESULT', type: 'select', value: 1, valueMap: {1:'合格', 2: '不合格'}}
        ]
    });
    listGrid.hideField('XMID');
    listGrid.hideField('WLID');
    listGrid.hideField('DETAILID');
    listGrid.hideField('GYSID');
    listGrid.hideField('CKID');
    listGrid.hideField('KWID');
    listGrid.hideField('DWID');
    listGrid.hideField('YSID');
    listGrid.hideField('CMID');
    listGrid.hideField('CGCODE');
    listGrid.hideField('CGID');
    for (var i = 0;i < data.length;i ++) {
        data[i]['RESULT'] = "1";
        data[i]['QTY_TEST'] = qtyTest;
    }
    listGrid.setData(data);

    var submitBtn = isc.OBFormButton.create({
        id: 'okButton',
        title: '确定',
        action: function() {
            //保存
            listGrid.saveEdits();

            var result = listGrid.data;
            if(result.length == 0){
                isc.showMessage("无操作数据！");
                return;
            }
            var qty = parseInt(qtyTest);

            for (var i = 0; i < result.length; i++) {
                if(result[i].QTY && (isNaN(result[i].QTY * 1) || result[i].QTY * 1 < 0)){
                    isc.showMessage("检验数量填写错误！");
                    return;
                }
                if(result[i].QTY_ERROR && (isNaN(result[i].QTY_ERROR * 1) || result[i].QTY_ERROR * 1 < 0)){
                    isc.showMessage("不合格数量填写错误！" + result[i].QTY_ERROR);
                    return;
                }
                var re = result[i].RESULT;
                var qtyJy = result[i].QTY * 1;
                var qtyError = result[i].QTY_ERROR * 1;

                if (qty < qtyJy) {
                    isc.showMessage("检验数量不能大于到货数量！");
                    return;
                }
                if (qty < qtyError) {
                    isc.showMessage("不合格数量不能大于到货数量！");
                    return;
                }
                if (qtyError > qtyJy) {
                    isc.showMessage("不合格数量不能大于检验数量！");
                    return;
                }
                if(!re){
                    isc.showMessage("请填写检验结果！");
                    return;
                }
            }
            var callback = function(
                rpcResponse,
                data,
                rpcRequest) {
                if (data.result) {
                    isc.showMessage(data.msg,isc.OBMessageBar.TYPE_INFO);
                    QualityView.refresh();
                    createDeliveryNote_Window.closeClick();
                } else {
                    isc.showMessage(data.msg,isc.OBMessageBar.TYPE_INFO);
                }
            };
            OB.RemoteCallManager.call("com.mes.handler.QualityQueryResultHandler",{result: result}, {}, callback);
        }
    });

    var createDeliveryNote_Window = isc.Window.create({
        id: "createDeliveryNote_Window",
        width: 320,
        height: 100,
        title: '来料检验明细',
        canDragResize: false,
        canDragReposition: true,
        showCloseButton: true,
        showMinimizeButton: false,
        autoCenter:true,
        autoSize:true,
        isModal:true,
        items: [
            isc.VLayout.create({
                defaultLayoutAlign: "center",
                align: "center",
                width: "100%",
                height: "100%",
                layoutMargin: 15,
                membersMargin: 70,
                members: [
                    listGrid,
                    isc.HLayout.create({
                        defaultLayoutAlign: "center",
                        align: "center",
                        height: 50,
                        membersMargin: 15,
                        marginBottom: 10,
                        members: [
                            submitBtn,
                            isc.OBFormButton.create({
                                id: 'cancelButton',
                                title: '取消',
                                action: function() {
                                    createDeliveryNote_Window.closeClick();
                                }
                            })
                        ]
                    })
                ]
            })
        ]
    });
    createDeliveryNote_Window.show();
}
